package com.talkweb.newproject.admin.rest;

import com.talkweb.newproject.admin.sysrolemapping.dto.SysRoleMappingDTO;
import com.talkweb.newproject.admin.sysrolemapping.service.SysRoleMappingService;
import com.talkweb.newproject.api.model.admin.SysRoleMappingParam;
import com.talkweb.newproject.api.model.common.ResponseResult;
import com.talkweb.newproject.api.model.common.Status;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by qsf on 2019/5/8.
 */
@RestController
@RequestMapping(value="/api/sysRoleMapping")
@Slf4j
public class SysRoleMappingRest {
    @Autowired
    private SysRoleMappingService sysRoleMappingService;

    /**
     * 根据id查询对象
     * @param id
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/get/v1/{id}",method = RequestMethod.POST)
    public ResponseResult<SysRoleMappingDTO> querySysRoleMappingByPrimaryKey(@PathVariable("id") String id) throws Exception {
        ResponseResult<SysRoleMappingDTO> result = new ResponseResult<>();
        try {
            SysRoleMappingDTO sysRoleMappingDTO = sysRoleMappingService.querySysRoleMappingByPrimaryKey(id);
            result.setStatus(Status.SUCCESS);
            result.setData(sysRoleMappingDTO);
            result.setMessage("查询成功!");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }
    }

    /**
     * 取得list对象
     * @param obj
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/getSysRoleMappingList",method = RequestMethod.POST)
    public Object querySysRoleMappingList(@RequestBody SysRoleMappingParam obj) throws Exception {
        return sysRoleMappingService.searchSysRoleMapping(obj);
    }

    /**
     * 根据条件查询信息
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/queryLike",method = RequestMethod.POST)
    public ResponseResult<SysRoleMappingDTO> queryLike(SysRoleMappingDTO dto) throws Exception {
        ResponseResult<SysRoleMappingDTO> result = new ResponseResult<>();
        try{
            SysRoleMappingDTO sysRoleMappingDTO = sysRoleMappingService.queryLikeSysRoleMapping(dto);
            result.setStatus(Status.SUCCESS);
            result.setData(sysRoleMappingDTO);
            result.setMessage("查询成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试!");
            return result;
        }

    }

    /**
     * 新增数据
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/create/v1",method = RequestMethod.POST)
    public ResponseResult create(@RequestBody SysRoleMappingDTO dto) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleMappingService.insertSysRoleMapping(dto);
            result.setStatus(Status.SUCCESS);
            result.setMessage("添加成功");
            return result;
        }catch(Exception e){
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /***
     * 更新数据
     * @param dto
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/update/v1",method = RequestMethod.POST)
    public ResponseResult update(SysRoleMappingDTO dto) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            sysRoleMappingService.updateSysRoleMapping(dto);
            result.setStatus(Status.SUCCESS);
            result.setMessage("修改成功");
            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }

    /**
     * 根据id删除数据
     * @param ids
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping(value="/delete/v1",method = RequestMethod.POST)
    public ResponseResult deleteSysRoleMapping(@RequestParam("ids") String ids) throws Exception {
        ResponseResult result = new ResponseResult();
        try{
            Boolean isDel = sysRoleMappingService.deleteSysRoleMapping(ids);
            if(isDel) {
                result.setStatus(Status.SUCCESS);
                result.setMessage("删除成功");
            }else {
                result.setStatus(Status.FAILD);
                result.setMessage("删除失败");
            }

            return result;
        }catch(Exception e) {
            log.error(e.getMessage(),e);
            result.setStatus(Status.FAILD);
            result.setMessage("执行异常,请重试");
            return result;
        }

    }
}
